<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

<script>
// 桶排序
// 把数据放到桶对应的索引位置，遍历所有桶取出有数据的桶的索引

var arr1 = [9,2,3,5,7,2,4];

/* var buckets = [];// 所有桶的数组

buckets[arr1[0]] = 'water';
buckets[arr1[1]] = 'water';
buckets[arr1[2]] = 'water';
buckets[arr1[3]] = 'water';
buckets[arr1[4]] = 'water';
buckets[arr1[5]] = 'water';

console.log( buckets );// [empty × 2, 'water', 'water', 'water', 'water', empty, 'water', empty, 'water']

// 清空原数组
arr1.length = 0;

for (var i in buckets){
    console.log( i );// '2' '3' '4' '5' '7' '9'
    arr1.push(Number(i));
}

console.log( arr1 );// [2, 3, 4, 5, 7, 9]
console.log( arr1.reverse() );// [9, 7, 5, 3, 2] */

function bucketSort(arr){
    var buckets = [];// 所有桶的数组
    // 分别取出原数组的值，作为buckets的索引并赋值
    for (var i = 0; i < arr.length; i++) {
        // 将数据放到对应的桶中
        buckets[arr[i]] = 'hehe';
    }
    // 清空原数组
    arr.length = 0;

    // 遍历数组，将有数据的桶的索引放到原数组中
    for (var index in buckets){
        arr.push(Number(index));
    }
    // 返回排序后的数组
    // return arr;// 升序
    return arr.reverse();// 降序
}

console.log( bucketSort([12,63,32,18,7,2,25]) );
console.log( bucketSort([100,32,62,2,53,253]) );

</script>
</body>
</html>